package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.facebook.drawee.view.SimpleDraweeView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.MyForumBean;
import mamikeji.liaowa.ui.activity.circle.PreviewCircleImgActivity;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForPreference;

/**
 * Created by 廉冲-android开发 on 2017/9/13.
 */
public class Main_ForumAdapter extends RecyclerView.Adapter<Main_ForumAdapter.ViewHolder> {
    List<MyForumBean.DataEntity> beanList;
    private Context mContext;
    OnItemClickListener mOnItemClickListener;
    View view;

    public Main_ForumAdapter(Context context, List<MyForumBean.DataEntity> beanList) {
        mContext = context;
        this.beanList = beanList;
    }

    public void addAllData(List<MyForumBean.DataEntity> beanList) {
        this.beanList = beanList;
        notifyDataSetChanged();
    }

    public void setItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);

        void onZanClick(int position);

        void onGuanZhuClick(int position);

        void onMSGClick(int position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_forum_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        final MyForumBean.DataEntity dataEntity = beanList.get(position);

        viewHolder.linearLayout_my_forum_img_guanzhu_main.setVisibility(ForPreference.getString(
                ForContents.USER_CODE).equals(dataEntity.getUsercode()) ? View.GONE : View.VISIBLE);
        viewHolder.linearLayout_my_forum_img_guanzhu_main.setBackgroundResource(dataEntity.getIs_follow() == 2 ?
                R.drawable.shape_momcircle_guanzhu_btn_bg_sel : R.color.color_ffffff);
        viewHolder.imageView_my_forum_guanzhu_main.setVisibility(dataEntity.getIs_follow() == 2 ? View.VISIBLE : View.GONE);
        switch (dataEntity.getIs_follow()) {
            case 1:
                viewHolder.textView_my_forum_guanzhu_main.setTextColor(mContext.getResources().getColor(R.color.color_text_333));
                viewHolder.textView_my_forum_guanzhu_main.setText(R.string.momcircle_guanzhu_1);
                break;
            case 2:
                viewHolder.textView_my_forum_guanzhu_main.setTextColor(mContext.getResources().getColor(R.color.color_ffffff));
                viewHolder.textView_my_forum_guanzhu_main.setText(R.string.momcircle_guanzhu_2);
                break;
            case 3:
                viewHolder.textView_my_forum_guanzhu_main.setTextColor(mContext.getResources().getColor(R.color.color_text_333));
                viewHolder.textView_my_forum_guanzhu_main.setText(R.string.momcircle_guanzhu_3);
                break;
        }

        viewHolder.textView_my_forum_contents_main.setText(dataEntity.getTopic_content());
        if (dataEntity.getTopic_content().length() >= 20) {
            viewHolder.textView_my_forum_contents_main.setLines(2);
            viewHolder.textView_my_forum_contents_main.setEllipsize(TextUtils.TruncateAt.END);
        }

        viewHolder.imageView_my_forum_identity_main.setImageResource(dataEntity.getCertification().equals("") ? R.drawable.icon_renzheng_n : R.drawable.icon_renzheng_h);
        viewHolder.imageView_my_forum_zannum_main.setImageResource(dataEntity.getIs_praise() == 1 ? R.drawable.icon_dianzan_h : R.drawable.icon_dianzan_n);
        viewHolder.textView_my_forum_nickname_main.setText(dataEntity.getNickname());
        viewHolder.textView_my_forum_position_main.setText(dataEntity.getCertification());
        viewHolder.textView_my_forum_title_main.setText(dataEntity.getTopic_title());
        viewHolder.textView_my_forum_zannum_main.setText(dataEntity.getPraise_total() + "");
        viewHolder.textView_my_forum_recommend_main.setText(dataEntity.getMessage_total() + "");
        viewHolder.textView_my_forum_browsenum_main.setText(dataEntity.getBrowse_total() + "");
        viewHolder.textView_my_forum_creattime_main.setText(mistiming(dataEntity.getPublishdate()));

        if (dataEntity.getTopic_content_type() == 1) {
            viewHolder.imageView_my_forum_img_main.setVisibility(View.GONE);
        } else {
            if (!dataEntity.getTopic_image().equals("") && !dataEntity.getTopic_image().equals("null") && dataEntity.getTopic_image() != null) {
                viewHolder.imageView_my_forum_img_main.setVisibility(View.VISIBLE);
                Glide
                        .with(mContext)
                        .load(dataEntity.getTopic_image().split(",")[0] + "?from=singlemessage")
                        .error(R.drawable.img_dongtaitu)
                        .into(viewHolder.imageView_my_forum_img_main);
            } else {
                viewHolder.imageView_my_forum_img_main.setVisibility(View.GONE);
            }
        }

        viewHolder.imageView_my_forum_titleimg_main.setImageURI(Uri.parse(dataEntity.getPhoto() + "?from=singlemessage"));

        //头像点击事件
        viewHolder.imageView_my_forum_titleimg_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onMSGClick(position);
            }
        });
        //关注点击事件
        viewHolder.linearLayout_my_forum_img_guanzhu_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dataEntity.getIs_follow() == 2) {
                    mOnItemClickListener.onGuanZhuClick(position);
                }
            }
        });
        //点赞点击事件
        viewHolder.imageView_my_forum_zannum_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onZanClick(position);
            }
        });
        //查看大图
        viewHolder.imageView_my_forum_img_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<String> stringList = new ArrayList<String>();
                stringList.add(dataEntity.getTopic_image().split(",")[0]);
                mContext.startActivity(
                        new Intent(mContext, PreviewCircleImgActivity.class)
                                .putStringArrayListExtra("urlList", stringList)
                                .putExtra("position", 1));
            }
        });
        viewHolder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public View mView;

        public TextView textView_my_forum_nickname_main;
        public TextView textView_my_forum_position_main;
        public TextView textView_my_forum_guanzhu_main;
        public TextView textView_my_forum_title_main;
        public TextView textView_my_forum_contents_main;
        public TextView textView_my_forum_zannum_main;
        public TextView textView_my_forum_recommend_main;
        public TextView textView_my_forum_browsenum_main;
        public TextView textView_my_forum_creattime_main;

        public SimpleDraweeView imageView_my_forum_titleimg_main;
        public ImageView imageView_my_forum_identity_main;
        public ImageView imageView_my_forum_guanzhu_main;
        public ImageView imageView_my_forum_img_main;
        public ImageView imageView_my_forum_zannum_main;

        LinearLayout linearLayout_my_forum_img_guanzhu_main;

        public ViewHolder(View view) {
            super(view);
            mView = view;

            textView_my_forum_nickname_main = (TextView) view.findViewById(R.id.textView_my_forum_nickname_main);
            textView_my_forum_position_main = (TextView) view.findViewById(R.id.textView_my_forum_position_main);
            textView_my_forum_guanzhu_main = (TextView) view.findViewById(R.id.textView_my_forum_guanzhu_main);
            textView_my_forum_title_main = (TextView) view.findViewById(R.id.textView_my_forum_title_main);
            textView_my_forum_contents_main = (TextView) view.findViewById(R.id.textView_my_forum_contents_main);
            textView_my_forum_zannum_main = (TextView) view.findViewById(R.id.textView_my_forum_zannum_main);
            textView_my_forum_recommend_main = (TextView) view.findViewById(R.id.textView_my_forum_recommend_main);
            textView_my_forum_browsenum_main = (TextView) view.findViewById(R.id.textView_my_forum_browsenum_main);
            textView_my_forum_creattime_main = (TextView) view.findViewById(R.id.textView_my_forum_creattime_main);

            imageView_my_forum_titleimg_main = (SimpleDraweeView) view.findViewById(R.id.imageView_my_forum_titleimg_main);
            imageView_my_forum_identity_main = (ImageView) view.findViewById(R.id.imageView_my_forum_identity_main);
            imageView_my_forum_guanzhu_main = (ImageView) view.findViewById(R.id.imageView_my_forum_guanzhu_main);
            imageView_my_forum_img_main = (ImageView) view.findViewById(R.id.imageView_my_forum_img_main);
            imageView_my_forum_zannum_main = (ImageView) view.findViewById(R.id.imageView_my_forum_zannum_main);

            linearLayout_my_forum_img_guanzhu_main = (LinearLayout) view.findViewById(R.id.linearLayout_my_forum_img_guanzhu_main);
        }
    }

    //判断时间差：新闻发布时间与当前系统时间的差值：如果小于60分钟换算成分钟；大于60分钟，换算成小时；大于24小时，换算成天
    public String mistiming(String time) {
        String date1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());

        SimpleDateFormat CurrentTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

        Date beginTime = null;
        try {
            beginTime = CurrentTime.parse(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date endTime = null;
        try {
            endTime = CurrentTime.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //进行判断
        Long shijianCha = beginTime.getTime() - endTime.getTime();
        int count = 0;
        if (shijianCha > 24 * 60 * 60 * 1000) {
            count = (int) (shijianCha / (24 * 60 * 60 * 1000));
            return count + "天前";
        } else if (shijianCha > 60 * 60 * 1000) {
            count = (int) (shijianCha / (60 * 60 * 1000));
            return count + "小时前";
        } else if (shijianCha > 60 * 1000) {
            count = (int) (shijianCha / (60 * 1000));
            return count + "分钟前";
        } else {
            return "刚刚";
        }
    }
}
